package com.atguigu.book.dao.impl;

import com.atguigu.book.dao.BookDAO;
import com.atguigu.book.pojo.Book;
import com.atguigu.myssm.basedao.BaseDAO;

import java.util.List;

/**
 * @ClassName: BookDAOImpl
 * @Description:
 * @Author: wty
 * @Date: 2022/12/12
 */

public class BookDAOImpl extends BaseDAO<Book> implements BookDAO {
    @Override
    public List<Book> getBookList(Integer minPrince, Integer maxPrice, Integer pageNo) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(" SELECT * FROM bookdb.`t_book` a where a.bookStatus = '0' ");
        if (null != minPrince && null != maxPrice) {
            stringBuffer.append("  and a.price BETWEEN ? AND ? LIMIT ?,10");
            return executeQuery(stringBuffer.toString(), minPrince, maxPrice, (pageNo - 1) * 10);
        } else if (null == minPrince && null != maxPrice) {
            stringBuffer.append("  and a.price <= ? LIMIT ?,10");
            return executeQuery(stringBuffer.toString(), maxPrice, (pageNo - 1) * 10);
        } else if (null != minPrince && null == maxPrice) {
            stringBuffer.append("  and a.price >= ? LIMIT ?,10");
            return executeQuery(stringBuffer.toString(), minPrince, (pageNo - 1) * 10);
        } else {
            stringBuffer.append(" LIMIT ?,10  ");
            return executeQuery(stringBuffer.toString(), (pageNo - 1) * 10);
        }
    }

    @Override
    public Book getBook(Integer id) {
        String sqlStr = "SELECT * FROM bookdb.`t_book` a WHERE a.id = ?";
        return load(sqlStr, id);
    }
}
